package com.jason;

import com.alibaba.fastjson.JSONObject;
import com.jason.constant.ResponseConstant;
import com.jason.entity.Token;
import com.jason.mapper.TokenMapper;
import com.jason.vo.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpCookie;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.util.MultiValueMap;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

@Component
public class LoginFilter implements GlobalFilter, Ordered {

    @Autowired
    private TokenMapper tokenMapper;

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        ServerHttpRequest request = exchange.getRequest();
        ServerHttpResponse response = exchange.getResponse();

        String path = request.getPath().toString();
        System.out.println("请求路径:" + path);
        //不做拦截
        if ("/user/login".equals(path) || "/user/register".equals(path) || "/code/create".equals(path)) {
            return chain.filter(exchange);
        }
        MultiValueMap<String, HttpCookie> cookies = request.getCookies();
        HttpCookie httpCookie = cookies.getFirst("cookieLogin");
        if (!ObjectUtils.isEmpty(httpCookie)) {
            String value = httpCookie.getValue();
            System.out.println("cookie 值" + value);
            if (StringUtils.isEmpty(value)) {
                //返回要求重新登录
                Response resultResponse = new Response();
                resultResponse.setCode(ResponseConstant.NOT_AUTH);
                DataBuffer wrap = response.bufferFactory().wrap(JSONObject.toJSONString(resultResponse).getBytes());
                return response.writeWith(Mono.just(wrap));
            }
            Token token = new Token();
            token.setToken(value);
            Token token1 = tokenMapper.selectOne(token);
            if (ObjectUtils.isEmpty(token1)) {
                //返回要求重新登录
                Response resultResponse = new Response();
                resultResponse.setCode(ResponseConstant.NOT_AUTH);
                DataBuffer wrap = response.bufferFactory().wrap(JSONObject.toJSONString(resultResponse).getBytes());
                return response.writeWith(Mono.just(wrap));
            }
            return chain.filter(exchange);
        } else {
            //返回要求重新登录
            Response resultResponse = new Response();
            resultResponse.setCode(ResponseConstant.NOT_AUTH);
            DataBuffer wrap = response.bufferFactory().wrap(JSONObject.toJSONString(resultResponse).getBytes());
            return response.writeWith(Mono.just(wrap));
        }

    }

    @Override
    public int getOrder() {
        return 0;
    }
}
